package com.banach.prema.repository;

import com.alibaba.fastjson.JSON;
import com.banach.prema.dto.query.AlarmPageQuery;
import com.banach.prema.dto.vo.AlarmVO;
import com.banach.prema.repository.dataobject.AlarmDO;
import com.banach.prema.repository.mapper.AlarmMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ego.framework.core.tookit.BeanToolkit;
import com.ego.framework.jdbc.repository.BaseRepository;
import com.ego.framework.model.CustomizePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AlarmRepository extends BaseRepository<AlarmMapper, AlarmDO, AlarmVO> {
    public CustomizePage<AlarmVO> selectPage(AlarmPageQuery pageQuery){
        LambdaQueryWrapper<AlarmDO> queryWrapper = Wrappers.<AlarmDO>lambdaQuery()
                .like(StringUtils.isNotBlank(pageQuery.getTitle()),AlarmDO::getTitle,pageQuery.getTitle())
                .eq(pageQuery.getUserId() != null,AlarmDO::getUserId,pageQuery.getUserId())
                .ge(pageQuery.getBeginTime()  != null,AlarmDO::getCreateTime, pageQuery.getBeginTime())
                .le(pageQuery.getEndTime()  != null,AlarmDO::getCreateTime, pageQuery.getEndTime())
                .eq(StringUtils.isNotBlank(pageQuery.getProvince()),AlarmDO::getProvince,pageQuery.getProvince())
                .eq(StringUtils.isNotBlank(pageQuery.getCity()),AlarmDO::getCity,pageQuery.getCity())
                .eq(StringUtils.isNotBlank(pageQuery.getDistrict()),AlarmDO::getDistrict,pageQuery.getDistrict())
                .orderByDesc(AlarmDO::getUpdateTime);
        IPage<AlarmDO> page = new Page(pageQuery.getPageNo(),pageQuery.getPageSize());
        page = this.baseMapper.selectPage(page, queryWrapper);
        CustomizePage<AlarmVO> customizePage = new CustomizePage(BeanToolkit.getInstance().copyList(page.getRecords(), AlarmVO.class), page.getSize(),page.getCurrent(), page.getTotal());
        return customizePage;
    }

}
